package com.dminvoicemanager;

/*Invoice Daily Sales summery report generatiing*/
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class DM_Inv_DailySalesSummary extends Activity {
	private String DBNAME = "dminvoicemanager.db";
	private String TEMP_USER = "invtempuser";
	private String ITEM_AREA = "fArea";
	private String TEMP_REP_INVOICESUMERY = "frepInvoiceSumery";
	private String ITEM_COMPANY_SETTING = "fCompanySetting";
	private String ITEM_CONTROL = "fControl";
	private String ITEM_DEBTOR = "fDebtor";
	private String ITEM_LOC_DETAILS = "fItemLoc";
	private String ITEM_LOCATION = "fLocation";
	private String ITEM_PRICE = "fItemPri";
	private String ITEMS = "fItems";
	private String INV_DETAILS = "FinvDet";
	private String INV_HEADER = "FinvHed";
	private SQLiteDatabase mydb;
	TextView VisitDate;
	String currentInvoiceRefCode;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dm_inv_daily_sales_summary);
		{
			VisitDate = (TextView) findViewById(R.id.visitdatetime);
			VisitDate.setText(getCurrentTimeString().toString());
			// Generate Daily Sales Summery Report
			showDailySalesSummary();
		}
	}

	private void showDailySalesSummary() {
		// TODO Auto-generated method stub
		ListView lvtypedes = (ListView) findViewById(R.id.dminvoicesalessummery);
		// create the grid item mapping
		String[] fromtrdes = new String[] { "coldes_1", "coldes_2", "coldes_3",
				"coldes_4", "coldes_5" };
		int[] totrdes = new int[] { R.id.dscuscode, R.id.dminvcusname,
				R.id.dminvoiceno, R.id.dminvnoofitems, R.id.dminvtotamount };
		// prepare the list of all records
		List<HashMap<String, String>> fillMapstrdes = new ArrayList<HashMap<String, String>>();

		SQLiteDatabase mydb;
		mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE, null);
		Cursor allrowitemsdes = null;
		String columsup[] = { "refInvoiceDetCode" };
		String usernm = "finac";
		try {
			allrowitemsdes = mydb.query(TEMP_REP_INVOICESUMERY, null,
					"repusername='" + usernm + "' AND invoiceComplete='2'",
					null, null, null, null);
		} catch (Exception e) {
		}
		if (allrowitemsdes != null) {
			if (allrowitemsdes.getCount() > 0) {
				while (allrowitemsdes.moveToNext()) {
					// ItemCode = 5 /cases 12 /pieces 14
					String rawInvoiceCustomerCode = allrowitemsdes
							.getString(11);
					String rawInvoiceCustomerName = allrowitemsdes
							.getString(12);
					String rawInvoiceSerialCode = allrowitemsdes.getString(3);
					String rawInvoiceSerialNo = allrowitemsdes.getString(4);
					// Formating invoice number
					try {
						if (rawInvoiceSerialNo.length() == 1) {
							String newInvNumberStr = "00000"
									+ rawInvoiceSerialNo;
							currentInvoiceRefCode = rawInvoiceSerialCode + "/"
									+ newInvNumberStr;
						} else if (rawInvoiceSerialNo.length() == 2) {
							String newInvNumberStr = "0000"
									+ rawInvoiceSerialNo;
							currentInvoiceRefCode = rawInvoiceSerialCode + "/"
									+ newInvNumberStr;
						} else if (rawInvoiceSerialNo.length() == 3) {
							String newInvNumberStr = "000" + rawInvoiceSerialNo;
							currentInvoiceRefCode = rawInvoiceSerialCode + "/"
									+ newInvNumberStr;
						} else if (rawInvoiceSerialNo.length() == 4) {
							String newInvNumberStr = "00" + rawInvoiceSerialNo;
							currentInvoiceRefCode = rawInvoiceSerialCode + "/"
									+ newInvNumberStr;
						} else if (rawInvoiceSerialNo.length() == 5) {
							String newInvNumberStr = "0" + rawInvoiceSerialNo;
							currentInvoiceRefCode = rawInvoiceSerialCode + "/"
									+ newInvNumberStr;
						} else {
							String newInvNumberStr = rawInvoiceSerialNo;
							currentInvoiceRefCode = rawInvoiceSerialCode + "/"
									+ newInvNumberStr;
						}
					} catch (Exception e) {
						Toast.makeText(getApplicationContext(),
								"Error Occured..", Toast.LENGTH_SHORT).show();
					}

					String rawInvoiceValue = allrowitemsdes.getString(7);
					String rawInvNoOfItems = allrowitemsdes.getString(19);

					/*
					 * String rawitemsdet = rawItemRef + "/" + rawItemCode + "/"
					 * + rawItemCases + "/" + rawItemPieces + "/" +
					 * rawItemDescription;
					 */

					HashMap<String, String> maptrdes = new HashMap<String, String>();
					maptrdes.put("coldes_1", rawInvoiceCustomerCode);
					maptrdes.put("coldes_2", rawInvoiceCustomerName);
					maptrdes.put("coldes_3", currentInvoiceRefCode);
					maptrdes.put("coldes_4", rawInvNoOfItems);
					maptrdes.put("coldes_5", rawInvoiceValue);
					fillMapstrdes.add(maptrdes);

				}
				// fill in the grid_item layout
				SimpleAdapter adaptertrdes = new SimpleAdapter(this,
						fillMapstrdes,
						R.layout.dm_daily_sales_summery_list_raw, fromtrdes,
						totrdes);
				lvtypedes.setAdapter(adaptertrdes);
			}
		}
		allrowitemsdes.close();
		mydb.close();
	}

	// Call current data and time
	private String getCurrentTimeString() {
		Calendar c = Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String formattedDate = df.format(c.getTime());
		return formattedDate;
	}
}
